package com.jk.controller.account;

import com.jk.dto.JsonResult;
import com.jk.entity.account.AccDepartment;
import com.jk.service.account.AccDepartmentService;
import com.jk.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 *  Created by yanghuan on 15-8-31.
 */
@Controller
@RequestMapping("/account/department")
public class DepartmentController {
    @Resource
    private AccDepartmentService accDepartmentService;

    //跳转到部门管理
    @RequestMapping("/page")
    public String departmentManage() {
        return "/account/department";
    }

    //获取显示的部门
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public String getList() {
        try {
            return JsonUtil.toString(accDepartmentService.selectAll());
        }catch (Exception e){
            e.printStackTrace();
            return JsonUtil.toString(new ArrayList<AccDepartment>());
        }
    }

    //创建权限
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String createDepartment(AccDepartment accDepartment) {
        try {
            accDepartmentService.insert(accDepartment);
            return JsonUtil.toString(new JsonResult(200, "保存成功"));
        }catch (Exception e){
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "保存失败"));
        }
    }

    //删除权限
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public String delDepartment(String id) {
        try {
            accDepartmentService.delete(id);
            return JsonUtil.toString(new JsonResult(200, "删除成功"));
        }catch (Exception e){
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "删除失败"));
        }
    }

}
